package com.leoman.service.impl;

import com.leoman.core.Constant;
import com.leoman.dao.BannerDao;
import com.leoman.entity.Banner;
import com.leoman.service.BannerService;
import com.leoman.utils.JsonUtil;
import com.leoman.utils.QiNiuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartRequest;

import java.util.List;

/**
 * BannerServiceImpl
 * Created by 涂奕恒 on 2016/10/18 0018 下午 3:16.
 */
@Service
public class BannerServiceImpl implements BannerService {

    @Autowired
    private BannerDao bannerDao;

    @Override
    public List<Banner> findAll() {
        return bannerDao.findAll();
    }

    @Override
    public Page<Banner> find(int pageNum, int pageSize) {
        return bannerDao.findAll(new PageRequest(pageNum - 1, pageSize, Sort.Direction.DESC, "id"));
    }

    @Override
    public Page<Banner> find(int pageNum) {
        return find(pageNum, Constant.PAGE_DEF_SIZE);
    }

    @Override
    public Banner getById(int id) {
        return bannerDao.findOne(id);
    }

    @Override
    public void deleteById(int id) {
        Banner banner = getById(id);

        if (null != banner) {
            QiNiuUtils.delete(banner.getCover());

            bannerDao.delete(id);
        }
    }

    @Override
    public Banner create(Banner banner) {
        return bannerDao.save(banner);
    }

    @Override
    public Banner update(Banner banner) {
        return bannerDao.save(banner);
    }

    @Override
    @Transactional
    public void deleteAll(int[] ids) {
        for (int id : ids) {
            deleteById(id);
        }
    }

    @Override
    public List<Banner> iFindList() {
        return bannerDao.findAll();
    }

    @Override
    public Integer saveBanner(Integer bannerId, String linkUrl, Integer videoTypeId, Integer videoId, MultipartRequest multipartRequest) {
        try {
            Banner banner;

            if (null == bannerId) {
                banner = new Banner();
                banner.setCreateTime(System.currentTimeMillis());
            } else {
                banner = bannerDao.findOne(bannerId);
                banner.setUpdateTime(System.currentTimeMillis());
            }

            MultipartFile multipartFile = multipartRequest.getFile("cover");
            if (null != multipartFile && multipartFile.getSize() > 0) {
                if (null != banner.getId()) {
                    QiNiuUtils.delete(banner.getCover());
                }
                banner.setCover(QiNiuUtils.upload(multipartFile));
            }

            banner.setLinkUrl(linkUrl);
            banner.setTypeId(videoTypeId);
            banner.setVideoId(videoId);

            bannerDao.save(banner);

            return 1;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    @Transactional
    public Integer batchDelete(String ids) {
        try {
            Integer[] bannerIds = JsonUtil.json2Obj(ids, Integer[].class);

            for (Integer bannerId : bannerIds) {
                deleteById(bannerId);
            }

            return 1;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }
}